﻿using NCS.IConnect.Core.WebApi.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NCS.IConnect.Core.Utilities;
using System.Net;

namespace NCS.IConnect.Core.WebApi.Description
{
    /// <summary>
    /// A custom <see cref="ResponseAttribute"/> is used to describe error based response.
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class ErrorResponseAttribute : ResponseAttribute
    {
        /// <summary>
        /// Create a new <see cref="ErrorResponseAttribute"/>.
        /// </summary>
        /// <param name="conditionCode">The condition code.</param>
        /// <param name="conditionDescription">The condition description.</param>
        /// <param name="errorCode">The error code for error response.</param>
        public ErrorResponseAttribute(string conditionDescription, string errorCode, string conditionCode = null)
        {
            ErrorInfo errorInfo;
            if (!Errors.TryGetError(errorCode, out errorInfo))
            {
                throw new ArgumentException(Resources.ExceptionInvalidErrorCode.Format(new object[] { errorCode }), "errorCode");
            }

            this.ErrorCode = errorCode;
            this.Condition = new ConditionInfo(conditionCode, conditionDescription);
            this.StatusCode = errorInfo.StatusCode;
        }

        /// <summary>
        /// Create a new <see cref="ErrorResponseAttribute"/>.
        /// </summary>
        ///<param name="statusCode">Status code.</param>
        /// <param name="conditionCode">The condition code.</param>
        /// <param name="conditionDescription">The condition description.</param>
        /// <param name="errorCode">The error code for error response.</param>
        public ErrorResponseAttribute(HttpStatusCode statusCode, string conditionDescription, string errorCode, string conditionCode = null)
            : base(statusCode, conditionDescription, errorCode, conditionCode)
        { }
    }
}
